const lark = require('@larksuiteoapi/node-sdk');
const { yingmenkou } = require('../utils/area_record');
let config = {};
// 开发者复制该Demo后，需要修改Demo里面的"app id", "app secret"为自己应用的appId, appSecret
let client = null;
process.nextTick(() => {
    config = {
        fujin: {
            appToken: process.env.FUJIN_APP_TOKEN,
            otaTableId: process.env.FUJIN_OTA_TABLE_ID,
            areaTableId: process.env.FUJIN_AREA_TABLE_ID,
            rankTableID: process.env.FEISHU_RANK_TABLE_ID,
            billTableId: process.env.FUJIN_BILL_TABLE_ID,
			cateringTableId: process.env.FUJIN_CATERING_TABLE_ID
        },
        yingmenkou: {
            appToken: process.env.YINGMENKOU_APP_TOKEN,
            otaTableId: process.env.YINGMENKOU_OTA_TABLE_ID,
            areaTableId: process.env.YINGMENKOU_AREA_TABLE_ID,
            rankTableID: process.env.FYINGMENKOU_RANK_TABLE_ID,
            billTableId: process.env.YINGMENKOU_BILL_TABLE_ID,
			cateringTableId: process.env.YINGMENKOU_CATERING_TABLE_ID
        },
        huarun: {
            appToken: process.env.HUARUN_APP_TOKEN,
            otaTableId: process.env.HUARUN_OTA_TABLE_ID,
            areaTableId: process.env.HUARUN_AREA_TABLE_ID,
            rankTableID: process.env.FHUARUN_RANK_TABLE_ID,
            billTableId: process.env.HUARUN_BILL_TABLE_ID,
			cateringTableId: process.env.HUARUN_CATERING_TABLE_ID
        },
		hongguangli: {
			appToken: process.env.HONGGUANGLI_APP_TOKEN,
            otaTableId: process.env.HONGGUANGLI_OTA_TABLE_ID,
            areaTableId: process.env.HONGGUANGLI_AREA_TABLE_ID,
            rankTableID: process.env.FHONGGUANGLI_RANK_TABLE_ID,
            billTableId: process.env.HONGGUANGLI_BILL_TABLE_ID,
			cateringTableId: process.env.HONGGUANGLI_CATERING_TABLE_ID
		}
    }
    client = new lark.Client({
        appId: process.env.FEISHU_APP_ID,
        appSecret: process.env.FEISHU_APP_SECRET,
        disableTokenCache: false
    });
})


// 增加团购
const addOTARecord = async (store, otaData) => {
	try {
		const data = await client.bitable.v1.appTableRecord.batchCreate({
			path: {
				app_token: config[store].appToken,
				table_id: config[store].otaTableId,
			},
			data: {
				records: otaData
			},
		})
	} catch(e) {
		console.log('ota error:', e)
	}
}
// 增加区域数据
const addArea = async (store, areaData) => {
	try {
		const data = await client.bitable.v1.appTableRecord.batchCreate({
			path: {
				app_token: config[store].appToken,
				table_id: config[store].areaTableId,
			},
			data: {
				records: areaData
			},
		})
	} catch(e) {
		console.log(e);
	}
}
// 增加充送
const addRank = async (store, areaData) => {
    const data = await client.bitable.v1.appTableRecord.batchCreate({
		path: {
			app_token: config[store].appToken,
			table_id: config[store].rankTableID,
		},
		data: {
            records: areaData
		},
	})
}
const addBill = async (store, areaData) => {
    const data = await client.bitable.v1.appTableRecord.batchCreate({
		path: {
			app_token: config[store].appToken,
			table_id: config[store].billTableId,
		},
		data: {
            records: areaData
		},
	})
}
const addCatering = async (store, areaData) => {
    const data = await client.bitable.v1.appTableRecord.batchCreate({
		path: {
			app_token: config[store].appToken,
			table_id: config[store].cateringTableId,
		},
		data: {
            records: areaData
		},
	})
}


module.exports = { addOTARecord, addArea, addRank, addBill, addCatering };